package com.peaksport.pkms.learntest.controller;

import java.util.Date;

import javax.ws.rs.core.MediaType;

import org.apache.kafka.common.KafkaFuture;
import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.peaksport.framework.base.controller.BaseController;
import com.peaksport.framework.base.venum.BaseIntEnum;
import com.peaksport.framework.extend.kafka.PKKafkaProducer;
import com.peaksport.framework.metadata.filter.FilterInfo;
import com.peaksport.framework.metadata.filter.FilterItemInfo;
import com.peaksport.framework.metadata.selector.SelectorItemCollection;
import com.peaksport.framework.metadata.selector.SelectorItemInfo;
import com.peaksport.framework.metadata.view.EntityViewInfo;
import com.peaksport.framework.util.PKClassUtils;
import com.peaksport.framework.util.PKContextUtils;
import com.peaksport.framework.util.PKInvokeUtils;
import com.peaksport.framework.venum.BillBaseStatusEnum;
import com.peaksport.pkms.base.entity.KFProduceMsgEntity;
import com.peaksport.pkms.base.venum.KFMsgTypeEnum;
import com.peaksport.pkms.learntest.service.TestJPAService;
import com.peaksport.pkms.permit.entity.UserEntity;

import test.peaksport.framework.metadata.entity.TBillEntity;
import test.peaksport.framework.metadata.entity.TBillEntryEntity;

@RestSchema(schemaId="pkms-test-jpa")
@RequestMapping(value="test/testJpa",produces=MediaType.APPLICATION_JSON)
public class TestJPAController extends BaseController{
	
	@Autowired
	private TestJPAService service;
	
	
	@GetMapping("testEntitySave")
	public TBillEntity testEntitySave() {
		TBillEntity billEntity = new TBillEntity();
		billEntity.setNumber("testbill");
		billEntity.setBizDate(new Date());
		billEntity.setBaseStatus(BillBaseStatusEnum.DELETED);
		
		TBillEntryEntity entry1 = new TBillEntryEntity();
		entry1.setSeq(1);
		entry1.setParent(billEntity);
		//BaseIntEnum b = new BaseIntEnum();
		//b.setValue(10);
	//	entry1.setLineStatus(b);
		
		TBillEntryEntity entry2 = new TBillEntryEntity();
		entry2.setSeq(2);
		entry2.setParent(billEntity);
		billEntity.getEntrys().add(entry1);
		billEntity.getEntrys().add(entry2);

		//service.save(billEntity);
		
		KFProduceMsgEntity kfEntity = new KFProduceMsgEntity();
		UserEntity u = new UserEntity();
		u.setId("xxx");
		kfEntity.setSender(u);
		kfEntity.setMsgBody("mysg");
		kfEntity.setMsgId("msgid");
		kfEntity.setSendTime(new Date());
		kfEntity.setType(KFMsgTypeEnum.JSON);
		kfEntity.setSuccess(true);
		PKKafkaProducer s = (PKKafkaProducer) PKClassUtils.getBean(PKKafkaProducer.class);
		s.save(kfEntity);
		
		return billEntity;
	}
	@GetMapping("testGetEntity") 
	public TBillEntity testGetEntity() {
		EntityViewInfo entityViewInfo = new EntityViewInfo();
		SelectorItemCollection sic = new SelectorItemCollection();
		sic.addSelectorItemInfo(new SelectorItemInfo("*"));
		sic.addSelectorItemInfo(new SelectorItemInfo("entrys.*"));
		entityViewInfo.setSelectorItemCol(sic);
		FilterInfo filterInfo = new FilterInfo();
		filterInfo.addFilterItemInfo(new FilterItemInfo("id","TLM/Q+h/Rr+qihLqUe6Tily2lGM="));
		entityViewInfo.setFilterInfo(filterInfo);
		TBillEntity bill = service.getValue(entityViewInfo, TBillEntity.class);
		return bill;
		
	}
	
}
